package gui;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPasswordField;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.SwingConstants;

import java.awt.Font;

import javax.swing.JComboBox;

import ClientControler.LoginController;
import javax.swing.ImageIcon;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {
	
	private JTextField userNameTxtField;
	private JLabel lblPassword;
	private JLabel lblUserName ;
	private JButton btnMaRegister;
	private JButton btnLogin;
	private JButton btnConnectToServer;
	private JPasswordField passwordLoginField;
	private JLabel lblWelcomeToSgi;
	private JLabel lblDate;
	private DateFormat dateFormat ;
	private Date date;
	private String type;
	private String userName;
	private String password;
	private String port;
	private String ip;
	private JLabel lblPort;
	private JLabel lblIp;
	private JTextField iPTextField;
	private JTextField portTextField;
	private JButton btnDisconnect;
	private LoginController loginController;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

					MainWindow window = new MainWindow();
					window.setVisible(true);

			}


	//Ct'r
	public MainWindow(){
		getContentPane().setFont(new Font("Monaco", Font.BOLD, 13));
		setResizable(false);
		
		initialize();

	}
	

	//Initialize the contents of the frame.

	private void initialize() {
		
		setBounds(250, 30, 800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		lblUserName = new JLabel("UserName:");
		lblUserName.setForeground(new Color(210, 105, 30));
		lblUserName.setFont(new Font("Monaco", Font.BOLD, 13));
		lblUserName.setBounds(404, 385, 83, 25);
		getContentPane().add(lblUserName);
		userNameTxtField = new JTextField();
		userNameTxtField.setForeground(new Color(210, 105, 30));
		userNameTxtField.setBounds(489, 382, 125, 28);
		getContentPane().add(userNameTxtField);
		userNameTxtField.setColumns(10);
		
		final JComboBox mainLoginComboBox = new JComboBox();
		mainLoginComboBox.setForeground(new Color(210, 105, 30));
		mainLoginComboBox.setToolTipText("");
		mainLoginComboBox.setMaximumRowCount(2);
		mainLoginComboBox.setBounds(638, 401, 117, 27);
		getContentPane().add(mainLoginComboBox);
		mainLoginComboBox.addItem("Client");
		mainLoginComboBox.addItem("Employee");
		mainLoginComboBox.setSelectedIndex(0);
		
		dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		date = new Date();
		lblDate = new JLabel(dateFormat.format(date));
		lblDate.setForeground(new Color(210, 105, 30));
		lblDate.setBounds(16, 75, 117, 28);
		getContentPane().add(lblDate);
		
	    btnLogin = new JButton("Login");
		btnLogin.setForeground(new Color(210, 105, 30));
		btnLogin.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent arg0) {
				
			userName = userNameTxtField.getText();
			password = passwordLoginField.getText();
			ip = iPTextField.getText();
			port = portTextField.getText();
			
			
			if (userName.isEmpty() || password.isEmpty()){
				JOptionPane.showMessageDialog(null, "Some of the fields are empty please fill it ");
			}
			else{
				if(mainLoginComboBox.getSelectedIndex()==0)
					loginController.login(userName, password,0);
				else
					loginController.login(userName, password,1);
				}
			}
		});
		
		btnLogin.setBounds(433, 462, 83, 29);
		getContentPane().add(btnLogin);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setForeground(new Color(210, 105, 30));
		lblPassword.setFont(new Font("Monaco", Font.BOLD, 13));
		lblPassword.setBounds(404, 425, 83, 25);
		getContentPane().add(lblPassword);
		
		btnMaRegister = new JButton("Register");
		btnMaRegister.setForeground(new Color(210, 105, 30));
		btnMaRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				userNameTxtField.setText("");
				passwordLoginField.setText("");
				Register regiesterFrame = new Register();
				regiesterFrame.setVisible(true);
			}
		});
		btnMaRegister.setBounds(661, 515, 83, 29);
		getContentPane().add(btnMaRegister);
		
		passwordLoginField = new JPasswordField();
		passwordLoginField.setForeground(new Color(210, 105, 30));
		passwordLoginField.setBounds(489, 422, 125, 28);
		getContentPane().add(passwordLoginField);
		
		
		lblWelcomeToSgi = new JLabel("Welcome To Space Global Imaging (SGI) System");
		lblWelcomeToSgi.setFont(new Font("Papyrus", Font.BOLD, 23));
		lblWelcomeToSgi.setHorizontalTextPosition(SwingConstants.CENTER);
		lblWelcomeToSgi.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToSgi.setForeground(new Color(210, 105, 30));
		lblWelcomeToSgi.setBounds(129, 28, 559, 58);
		getContentPane().add(lblWelcomeToSgi);
		
		JButton btnPublicSearch = new JButton("Public Search");
		btnPublicSearch.setForeground(new Color(210, 105, 30));
		btnPublicSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PublicSearch publicSearchFrame = new PublicSearch();
				publicSearchFrame.setVisible(true);
			}
		});
		btnPublicSearch.setBounds(546, 515, 117, 29);
		getContentPane().add(btnPublicSearch);
		
		lblIp = new JLabel("IP:");
		lblIp.setForeground(new Color(210, 105, 30));
		lblIp.setFont(new Font("Monaco", Font.BOLD, 13));
		lblIp.setBounds(208, 286, 32, 16);
		getContentPane().add(lblIp);
		
		lblPort = new JLabel("Port:");
		lblPort.setForeground(new Color(210, 105, 30));
		lblPort.setFont(new Font("Monaco", Font.BOLD, 13));
		lblPort.setBounds(193, 314, 45, 16);
		getContentPane().add(lblPort);
		
		iPTextField = new JTextField();
		iPTextField.setFont(new Font("Monaco", Font.BOLD, 13));
		iPTextField.setForeground(new Color(210, 105, 30));
		iPTextField.setBounds(230, 280, 117, 28);
		getContentPane().add(iPTextField);
		iPTextField.setColumns(10);
		
		portTextField = new JTextField();
		portTextField.setForeground(new Color(210, 105, 30));
		portTextField.setFont(new Font("Monaco", Font.BOLD, 13));
		portTextField.setBounds(230, 308, 66, 28);
		getContentPane().add(portTextField);
		portTextField.setColumns(10);
		
		btnConnectToServer = new JButton("Connect To Server");
		btnConnectToServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				loginController = new LoginController();
				Boolean loginCheck = loginController.connect(ip, port);
				if (loginCheck){
					
					userNameTxtField.setVisible(true);
					passwordLoginField.setVisible(true);
					lblPassword.setVisible(true);
					lblUserName.setVisible(true);
					btnLogin.setVisible(true);
					btnDisconnect.setVisible(true);
					btnConnectToServer.setVisible(false);
					lblIp.setEnabled(false);
					lblPort.setEnabled(false);
						
				}
				
			}
		});
		btnConnectToServer.setFont(new Font("Monaco", Font.BOLD, 13));
		btnConnectToServer.setForeground(new Color(210, 105, 30));
		btnConnectToServer.setBounds(297, 309, 164, 29);
		getContentPane().add(btnConnectToServer);
		
		btnDisconnect = new JButton("Disconnect");
		btnDisconnect.setForeground(new Color(210, 105, 30));
		btnDisconnect.setFont(new Font("Monaco", Font.BOLD, 13));
		btnDisconnect.setBounds(297, 309, 117, 29);
		getContentPane().add(btnDisconnect);
		
		JLabel lblPictureFrame = new JLabel("");
		lblPictureFrame.setIcon(new ImageIcon(MainWindow.class.getResource("/FramePictures/mainwindow.jpg")));
		lblPictureFrame.setBounds(0, 0, 800, 578);
		getContentPane().add(lblPictureFrame);
	
	}
	
	public String GetPassword (){
		return password ;
	}
	
	public String GetUserName (){
		
		return userName ;
	}
	
	public String GetType (){
		
		return type ;
	}
}
	



